package com.company.indicationshots.adapter;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Context;
import android.graphics.Color;
import android.util.SparseBooleanArray;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.TextView;

import androidx.core.content.ContextCompat;

import com.company.indicationshots.R;
import com.company.indicationshots.model.Score;

import java.util.List;
//射靶历史记录列表数据加载适配器，此适配器未用到，可忽略
public class RecordListAdapter extends ArrayAdapter<Score> {
    private Context context;
    private int layoutResourceId;
    private List<Score> data;

    private int selectedPosition = 0; // 初始时没有选中的行

    public RecordListAdapter(Context context, int layoutResourceId, List<Score> data) {
        super(context, layoutResourceId, data);
        this.layoutResourceId = layoutResourceId;
        this.context = context;
        this.data = data;
    }

    // 添加设置选中位置的方法
    public void setSelectedPosition(int position) {
        this.selectedPosition = position;
        notifyDataSetChanged(); // 通知数据集变化，触发视图更新
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        View row = convertView;
        MyHolder holder = null;

        if (row == null) {
            LayoutInflater inflater = ((Activity) context).getLayoutInflater();
            row = inflater.inflate(layoutResourceId, parent, false);

            holder = new MyHolder();
            holder.recordText1 = row.findViewById(R.id.record_num);
            holder.recordText2 = row.findViewById(R.id.record_ring);
            holder.recordText3 = row.findViewById(R.id.record_distance);
            holder.recordText4 = row.findViewById(R.id.record_time);

            row.setTag(holder);

        } else {
            holder = (MyHolder) row.getTag();
        }

        // 设置行的背景颜色
        if (selectedPosition == position) {
            row.setBackgroundColor(Color.parseColor("#0e932e")); // 选中行背景颜色为绿色
        } else {
            row.setBackgroundColor(Color.BLACK); // 或其他默认颜色
        }

        Score score = data.get(position);
        String[] parts = (score.getNum() + "," + score.getRing() + "," + score.getDistance() + "," + score.getTime()).split(","); // 假设数据是以逗号分隔的

        holder.recordText1.setText(parts[0]);
        holder.recordText2.setText(parts[1]);
        holder.recordText3.setText(parts[2]);
        holder.recordText4.setText(parts[3]);

        return row;
    }

    static class MyHolder {
        TextView recordText1;
        TextView recordText2;
        TextView recordText3;
        TextView recordText4;
    }
}
